<?php

class UtilityController extends Controller
{
// 	public function filters()
// 	{
// 		return array(
// 			'accessControl', // perform access control for CRUD operations
// 		);
// 	}

// 	public function accessRules(){
// 	    return array();
// 	}

    public function actionUpload()
	{
		if ($_FILES["file"]["error"] > 0)
		{
			echo json_encode(array("status" => "failure", "msg" => $_FILES["file"]["error"]));
		}
		else
		{			
			$filePath = Utility::getTempUploadPath(true) . '/' . $_FILES["file"]["name"];
			move_uploaded_file($_FILES["file"]["tmp_name"], $filePath);
			
			$fileUrl = '/temp/upload/' . Utility::getSessionID() . '/' . $_FILES["file"]["name"];
			echo json_encode(array("status" => "success", "path" => $fileUrl));
		  }					 
	}
	
	public function actionRemove()
	{
		if(isset($_POST['fileNames']))
		{
			try{
				$filePath = Utility::getTempUploadPath(true) . "/" . $_POST['fileNames'];
				
				unlink($filePath);
				
				echo json_encode(array("status" => "success"));
			}
			catch(Exception $ex)
			{				
				echo json_encode(array("status" => "failure", "msg" => $ex -> getMessage()));
			}
		}
		else
		{
			echo json_encode(array("status" => "failure", "msg" => "fileNames is empty!"));
		}
	}
}